import pinia from '@/pinia/index.ts'
import { useUserStore } from '@/pinia/module/user';
import elTheme from '@/styles/theme-element';

const userStore = useUserStore(pinia);

// 定义主题类型集合
type ThemeType = keyof typeof elTheme;

// 确保 theme 属性值在有效范围内
if (!Object.keys(elTheme).includes(userStore.theme)) {
    console.error(`Invalid theme: ${userStore.theme}`);
    userStore.saveTheme('dark'); // 默认回退到 'dark'
}

// 修改主题
export function changeTheme () {
    // 修改状态 和 tailwindcss主题
    if (userStore.theme  === 'dark') { 
        userStore.saveTheme('high'); 
    }else{
        userStore.saveTheme('dark');
    }
    // 修改elementplus主题
    const themeCsslist = elTheme[userStore.theme as ThemeType];
    const root = document.documentElement;
    Object.keys(themeCsslist).forEach((key) => {
        root.style.setProperty(key, themeCsslist[key as keyof typeof themeCsslist]);
    });

};

// 初始化主题
export function initTheme (theme: string) {
    userStore.saveTheme(theme);
    // 修改状态 和 tailwindcss主题
    const themeCsslist = elTheme[userStore.theme as ThemeType];
    const root = document.documentElement;
    Object.keys(themeCsslist).forEach((key) => {
        root.style.setProperty(key, themeCsslist[key as keyof typeof themeCsslist]);
    });
}
